/* eslint-disable @typescript-eslint/no-unused-vars */
import { BasicForm, useForm, FormSchema } from "@/components/Form";
import { equipmentsList, techniqueList, secondmentsCreate, secondmentsList, staffRotationList } from "@/api/SecondmentDictionary";
import { message } from "antd";
import "./index.less";
import { useTranslation } from "react-i18next";
import { BasicTable, useTable, BasicColumn } from "@/components/Table/index";
import { useEffect, useState } from "react";
import { TimeDate } from "@/utils/util";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { arrDeconstruction } from "@/utils/util";
import { useFetchHistoryData, History } from "@/components/ViewComponents/History/index";
let start = 0;
let end = 0;
let isDate = false;
export default function referral_management() {
	const { t } = useTranslation();
	let btnList = [
		{
			key: "save"
		}
	];
	const formSchemaTop: FormSchema[] = [
		{
			label: t("felix_ui.decision_date"),
			field: "decisionDate", //决定日期
			component: "DatePicker"
		},
		{
			label: t("felix_ui.contract_date"),
			field: "contractDate", //合同签订日期
			component: "DatePicker"
		},
		{
			label: t("felix_ui.date_from"),
			field: "dateFrom", //开始日期
			component: "DatePicker",
			componentProps: {
				onChange: data => {
					const dataTime = data._d.getTime();
					start = dataTime;
				}
			}
		},
		{
			label: t("felix_ui.date_to"),
			field: "dateTo", //结束日期
			component: "DatePicker",
			componentProps: {
				onChange: data => {
					const dataTime = data._d.getTime();
					end = dataTime;
				}
			}
		},
		{
			label: t("felix_ui.numb_of_sheet"),
			field: "numbOfSheet", //票据代码
			component: "Input"
		},
		{
			label: t("felix_ui.decision_numb"),
			field: "decisionNumb", //决策编号
			component: "Input"
		},
		{
			label: t("felix_ui.contract_numb"),
			field: "contractNumb", //合同编号
			component: "Input",
			labelWidth: 120
		},

		{
			label: t("felix_ui.contract_notes"),
			field: "contractNotes", //备注
			component: "Input"
		},
		{
			label: t("felix_ui.secd_details"),
			field: "secdDetails", //内容 / 详情
			component: "Input"
		},
		{
			label: t("felix_ui.secd_reason"),
			field: "secdReason", //借调原因
			component: "Select",
			componentProps: {
				options: [
					{ label: "1" + t("secondmentDictionary.HEALTH"), value: "1" },
					{ label: "2" + t("secondmentDictionary.project"), value: "2" },
					{ label: "3" + t("secondmentDictionary.remote"), value: "3" }
				]
			},
			labelWidth: 120
		},
		{
			label: t("felix_ui.workplace"),
			field: "workplace", //出差地址
			component: "Input"
		},
		// {
		// 	field: "secd_reason",
		// 	component: "Select",
		// 	componentProps: {
		// 		options: [
		// 			{ label: "1", value: "1" },
		// 			{ label: "2", value: "2" }
		// 		]
		// 	},
		// 	colProps: { span: 8 }
		// },

		{
			label: t("felix_ui.support_details"),
			field: "supportDetails", //支持详情 / 支持内容
			component: "Input",
			labelWidth: 120
		},
		{
			label: t("felix_ui.support_date"),
			field: "supportDate", //日期（支援日期）
			component: "DatePicker",
			colProps: { span: 12 },
			componentProps: {
				onChange: (data: any) => {
					if (start === 0 || end === 0) {
						isDate = false;
						message.error(t("kevin_ui.pleaseSelect"));
						return;
					}
					console.log(data);
					const dataTime = data._d.getTime();
					console.log(dataTime);
					if (dataTime < start || dataTime > end) {
						isDate = false;
						message.error(t("kevin_ui.theSupport"));
						return;
					}
					isDate = true;
				}
			}
		}
		// {
		// 	label: t("felix_ui.support_technique"),
		// 	field: "supportTechnique", //支持技术
		// 	component: "Input",
		// 	colProps: { span: 24 }
		// }
	];
	const [formTop, { getFieldsValue: formTopHandleSubmit }] = useForm({
		schemas: formSchemaTop,
		showActionButtonGroup: false,
		labelWidth: 100,
		baseColProps: {
			span: 8
		}
	});
	const formSchemaBottom: FormSchema[] = [
		{
			label: t("felix_ui.pat_treated_count"),
			field: "patTreatedCount", //检查与治疗的患者数量
			component: "Input",
			colProps: { span: 12 },
			labelWidth: 200
		},
		{
			label: t("felix_ui.pat_surgery_count"),
			field: "patSurgeryCount", //进行手术的患者数量
			component: "Input",
			colProps: { span: 12 },
			labelWidth: 200
		},
		{
			label: t("felix_ui.support_evaluation"),
			field: "supportEvaluation", //结果评估
			component: "Select",
			componentProps: {
				options: [
					{ label: t("secondmentDictionary.excellen"), value: "1" },
					{ label: t("secondmentDictionary.good"), value: "2" },
					{ label: t("secondmentDictionary.fair"), value: "3" },
					{ label: t("secondmentDictionary.average"), value: "4" }
				]
			},
			colProps: { span: 24 },
			labelWidth: 120
		},
		{
			label: t("felix_ui.accomm_place"),
			field: "accommPlace", //住宿
			component: "Input",
			colProps: { span: 8 }
		},
		{
			label: t("felix_ui.accomm_fee"),
			field: "accommFee", //住宿money
			component: "Input",
			colProps: { span: 8 }
		},
		{
			label: t("felix_ui.secd_expense"),
			field: "secdExpense", //旅行费用
			component: "Input",
			colProps: { span: 8 }
		},
		{
			label: t("felix_ui.meal_allowance"),
			field: "mealAllowance", //餐费津贴
			component: "Input",
			colProps: { span: 8 }
		},
		{
			label: t("felix_ui.documents"),
			field: "documents", //文件（文件费用）
			component: "Input",
			colProps: { span: 8 }
		},
		{
			label: t("felix_ui.training_fee"),
			field: "trainingFee", //培训费用
			component: "Input",
			colProps: { span: 8 },
			labelWidth: 120
		},
		{
			label: t("felix_ui.other"),
			field: "other", //其他费用
			component: "Input",
			colProps: { span: 8 }
		}
	];
	const [formBottom, { getFieldsValue: formBottomHandleSubmit }] = useForm({
		schemas: formSchemaBottom,
		showActionButtonGroup: false,
		labelWidth: 120
	});
	const columns: BasicColumn[] = [
		{
			title: t("kevin_ui.technology"),
			dataIndex: "techName"
		}
	];
	const columnsRightTop: BasicColumn[] = [
		{
			title: t("felix_ui.sq_numb"),
			dataIndex: "index",
			render: (text, record, index) => index * 1 + 1 // 设置序号
		},
		{
			title: t("felix_ui.full_name"),
			dataIndex: "fullName",
			width: 160
		},
		{
			title: t("felix_ui.birthday"),
			dataIndex: "birthday"
		},
		{
			title: t("felix_ui.gender"),
			dataIndex: "gender"
		},
		{
			title: t("felix_ui.poz_level"),
			dataIndex: "pozLevel"
		},
		{
			title: t("felix_ui.fieldOfImplementation"),
			dataIndex: "fieldOfImplementation"
		},
		{
			title: t("felix_ui.date_from"),
			dataIndex: "dateFrom"
		},
		{
			title: t("felix_ui.date_to"),
			dataIndex: "dateTo"
		}
	];
	const columnsRightBottom: BasicColumn[] = [
		{
			title: t("kevin_ui.name"),
			dataIndex: "eqpName"
		},
		{
			title: t("kevin_ui.sign"),
			dataIndex: "eqpId"
		},
		{
			title: t("kevin_ui.version"),
			dataIndex: "genVno"
		},
		{
			title: t("kevin_ui.manufacturer"),
			dataIndex: "orgId"
		},
		{
			title: t("kevin_ui.generationDate"),
			dataIndex: "proDtime",
			render: (text: any) => {
				return TimeDate(text);
			}
		},
		{
			title: t("kevin_ui.purchaseDate"),
			dataIndex: "buyDtime",
			render: (text: any) => {
				return TimeDate(text);
			}
		},
		{
			title: t("kevin_ui.scrapDate"),
			dataIndex: "expDtime",
			render: (text: any) => {
				return TimeDate(text);
			}
		}
	];
	const schemas = [
		{
			label: t("kevin_ui.startTime"),
			field: "dateFrom"
		},
		{
			label: t("kevin_ui.endTime"),
			field: "dateTo"
		},
		{
			label: t("kevin_ui.second"),
			field: "ticketCode"
		},
		{
			label: t("kevin_ui.personResponsible"),
			field: "staffName"
		},
		{
			label: t("kevin_ui.secondmentAddress"),
			field: "workplace"
		},
		{
			label: t("kevin_ui.assessment"),
			field: "secdResults"
		}
	];
	const [registerTable, { reload, getSelectRows: registerTableGetSelectRows }] = useTable({
		api: techniqueList,
		columns,
		useSearchForm: false,
		showTableSetting: false,
		rowSelection: {
			type: "Checkbox",
			onChange: (techId: any) => {
				console.log(techId);
			}
		},
		bordered: true,
		showIndexColumn: true,
		canResize: false,
		maxHeight: 150
	});
	const [staffID, setStaffID] = useState("");
	const [referralNumber, setReferralNumber] = useState("");
	const [registerTableRightTop, { getSelectRows: registerTableRightTopGetelectRows }] = useTable({
		api: staffRotationList,
		columns: columnsRightTop,
		useSearchForm: false,
		showTableSetting: false,
		rowSelection: {
			type: "Checkbox",
			onSelect: ({ staffId }: object) => {
				staffID === staffId ? setStaffID("") : setStaffID(staffId);
			}
		},
		maxHeight: 150
	});
	const [registerTableRightBottom, { getSelectRows: registerTableRightBottomGetSelectRows }] = useTable({
		api: equipmentsList,
		columns: columnsRightBottom,
		useSearchForm: false,
		showTableSetting: false,
		rowSelection: {
			type: "Checkbox",
			onChange: () => {
				console.log("registerTableRightBottom");
			}
		},
		maxHeight: 150
	});
	const submit = async () => {
		if (!isDate) {
			message.error(t("kevin_ui.supportTimeError"));
			return;
		}
		const registerTableList = registerTableGetSelectRows();
		const registerTableRightTopList = registerTableRightTopGetelectRows();
		const registerTableRightBottomList = registerTableRightBottomGetSelectRows();
		//技术
		const secdTechss = arrDeconstruction(registerTableList, { techId: "techId" });
		//人员
		const secdStaffss = arrDeconstruction(registerTableRightTopList, { staffId: "staffId" });
		//设备
		const secdEqpss = arrDeconstruction(registerTableRightBottomList, { eqpId: "eqpId" });
		const formTopHandle = await formTopHandleSubmit();
		if (
			!formTopHandle.contractDate &&
			!formTopHandle.dateFrom &&
			!formTopHandle.dateTo &&
			!formTopHandle.decisionDate &&
			!formTopHandle.supportDate
		) {
			message.error(t("kevin_ui.cannotBeEmpty"));
			return;
		}
		formTopHandle.contractDate = formTopHandle.contractDate.split(" ")[0];
		formTopHandle.dateFrom = formTopHandle.dateFrom.split(" ")[0];
		formTopHandle.dateTo = formTopHandle.dateTo.split(" ")[0];
		formTopHandle.decisionDate = formTopHandle.decisionDate.split(" ")[0];
		formTopHandle.supportDate = formTopHandle.supportDate.split(" ")[0];
		const formBottom = await formBottomHandleSubmit();
		const res = await secondmentsCreate({ ...formTopHandle, ...formBottom, secdTechss, secdStaffss, secdEqpss });
		console.log(res);
		res ? message.success(t("kevin_ui.addedSuccessfully")) : message.error(t("kevin_ui.addFailed"));
		setReferralNumber(res);
	};
	const [register] = useFetchHistoryData({
		api: secondmentsList,
		contents: "ticketCode",
		describe: "Secondment Order Number：",
		url: "/SecondmentDictionary/102",
		id: referralNumber,
		schemas: schemas,
		parameter: {
			pageNo: 1,
			pageSize: 10
		}
	});
	const handlerToolbarAction = (key: string) => {
		switch (key) {
			case "save":
				// handleSubmitRegistration();
				submit();
				break;
		}
	};
	useEffect(() => {
		reload();
	}, []);
	return (
		<div className="referral_management_content">
			<div className="referral_management_left block">
				<div className="block-label">Ⅰ、{t("felix_ui.referralForm")}</div>
				<BasicForm className="formTop" register={formTop}></BasicForm>
				<BasicForm register={formBottom}></BasicForm>
				<div className="management_left_table">
					<div className="block-label">Ⅱ、{t("felix_ui.referralHistory")}</div>
					<History register={register}></History>
				</div>
			</div>
			<div className="referral_management_right block">
				<div className="Staff">
					<div className="block-label">Ⅲ、{t("felix_ui.staffTable")}</div>
					<div className="registerTableRightTop">
						<BasicTable register={registerTableRightTop}></BasicTable>
					</div>
				</div>
				<div className="Technology">
					<div className="block-label">Ⅳ、{t("felix_ui.technologyTable")}</div>
					<BasicTable register={registerTable}></BasicTable>
				</div>
				<div className="Device">
					<div className="block-label">Ⅴ、{t("felix_ui.deviceTable")}</div>
					<BasicTable register={registerTableRightBottom}></BasicTable>
				</div>
			</div>
			<Toolbar action={handlerToolbarAction} btnList={btnList} />
		</div>
	);
}
